

import * as groupProxy from '../../proxys/group'
import { CustomError } from '../../error'

export const getlist = (auth, req, res, next) => {
  return groupProxy.getList()
    .then( ret => res.api(ret) )
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}

export const edit = (data, req, res, next) => {
  let { query, info, auth } = data
  return groupProxy.editGroup(query, info)
    .then( ret => res.api(ret) )
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}

export const create = (data, req, res, next) => {
  let { info, auth } = data
  return groupProxy.createGroup(info)
    .then( ret => groupProxy.getList() )
    .then( ret => res.api(ret) )
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}

export const remove = (data, req, res, next) => {
  return groupProxy.removeGroup(data._id)
    .then( ret => groupProxy.getList() )
    .then( ret => res.api(ret) )
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}

export const setFlag = (data, req, res, next) => {
  let { query, info, auth } = data
  return groupProxy.editGroup(query, info)
    .then( ret => res.api(ret) )
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}